﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using ADministration.Web;
using ADministration.Commands;
using System.ServiceModel.DomainServices.Client;
using System.Collections;
using System.Collections.Generic;

namespace ADministration.ViewModels
{
    public class DeleteCategoryViewModel : ViewModelBase
    {
        private NavigationService navigationService;
        private TicketsDomainContext context;
        private NavigationContext NavigationContext;
        private Category category;

        public string DeleteConfirmMessage { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand CancelCommand { get; set; }

        public DeleteCategoryViewModel(NavigationService navigationService, NavigationContext NavigationContext)
        {
            this.context = new TicketsDomainContext();
            this.navigationService = navigationService;
            this.NavigationContext = NavigationContext;
            this.CancelCommand = new RelayCommand(OnCancel);
            this.DeleteCommand = new RelayCommand(OnDeleteCategory);

            int categoryID;
            try
            {
                categoryID = int.Parse(this.NavigationContext.QueryString["id"]);
            }
            catch (Exception e)
            {
                this.navigationService.GoBack();
                throw;
            }

            EntityQuery<Category> queryCategory = from c in context.GetCategoriesQuery()
                                                  where c.CategoryID == categoryID
                                                  select c;

            context.Load(queryCategory, OnGetCategoryByIdCompleted, null);
        }

        private void OnGetCategoryByIdCompleted(LoadOperation<Category> operation)
        {
            
            if (operation.HasError || (operation.Entities.Count() != 1))
            {
                this.navigationService.GoBack();
                throw operation.Error;
            }

            this.category = operation.Entities.First();

            this.DeleteConfirmMessage =
                "Please confirm deletion of category '" +
                this.category.Name + "'.";
            this.OnPropertyChanged("DeleteConfirmMessage");
        }

        private void OnDeleteCategory()
        {
            if (this.context != null && this.category != null)
            {
                this.context.Categories.Remove(this.category);
                this.context.SubmitChanges(OnDeleteCategoryComplited,null);
            }
        }
  
        private void OnDeleteCategoryComplited(SubmitOperation operation)
        {
            if (!operation.HasError)
            {
                this.navigationService.GoBack();
            }
        }

        private void OnCancel()
        {
            this.navigationService.GoBack();
        }
    }
}
